﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fitness_club
{
    public partial class ViewMember : Form
    {
        public static string constr = ConfigurationManager.AppSettings["ConStr"];
        SqlConnection con = new SqlConnection(constr);
        Boolean isActiveMem, isDeactiveMem;
        int navCode = -1;
       
        public ViewMember()
        {
            InitializeComponent();
            //bindingNavigator1.Visible = true;

        }

        public ViewMember(int navCode)
        {
            InitializeComponent();
            this.navCode = navCode;

        }

        private void ViewMember_Load(object sender, EventArgs e)
        {
            if (this.navCode == Constants.FORM_VIEWALL_CODE)
            {
                loadGrid(true);
                checkBoxActive.Checked = true;
                checkBoxDeactive.Checked = false;
            }
            else if (this.navCode == Constants.FORM_PAY_FEES)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
                checkBoxActive.Enabled = false;
                checkBoxDeactive.Enabled = false;
                buttonDeactivate.Enabled = false;
                loadGridforDueMember();
            }
        }
        private void loadGrid(Boolean isActiveMember)
        {
            String status = "A";
            String alerMsg = "No Active member found";
            if (!isActiveMember)
            {
                status = "I";
                alerMsg = "No Inactive member found";
                buttonEdit.Enabled = false;
                buttonPay.Enabled = false;
            }

            SqlDataAdapter adapter = new SqlDataAdapter("select m.id as Id,m.firstname as [First Name],m.lastname as [Last Name],m.due_date as [Due Date],m.last_paid_date as [Last Paid Date],sp.type as [Plan Type],sp.amount as Amount,sp.duration as Duration,m.contactno as [Contact No],m.dob as [Date of Birth],m.age as Age,m.address as Address,m.weight as Weight,m.height as Height,m.health_issues as [Health Issues],m.blood_pressure as [Blood Pressure],m.sugar as Sugar,m.dateofjoining as [Date of Joining] from member m LEFT JOIN subscription_plan sp ON m.mem_plan=sp.id where m.status='"+status+"'", con);
            DataTable dt = new DataTable();
            // con.Open();D:\Visual studio\project\GYM\Fitness_club\addinstructor.cs
            adapter.Fill(dt);
            dataGridView1.DataSource = dt;
            if (dt.Rows.Count == 0)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
                buttonPay.Enabled = false;
                buttonDeactivate.Enabled = false;
                //   DialogResult result = MessageBox.Show(alerMsg, "View Members", MessageBoxButtons.OK, MessageBoxIcon.Information);


            }
            else
            {
                if (isActiveMember)
                {
                    buttonEdit.Enabled = true;
                    buttonDelete.Enabled = true;
                    buttonPay.Enabled = true;
                }
                buttonDeactivate.Enabled = true;
            }
        }

        private void loadGridforDueMember()
        {
            SqlDataAdapter adapter = new SqlDataAdapter("select m.id as Id,m.firstname as [First Name],m.lastname as [Last Name],sp.type as [Plan Type],sp.amount as Amount,sp.duration as Duration,m.address as Address,m.contactno as [Contact No],m.dateofjoining as [Date of Joining],m.due_date as [Due Date],m.last_paid_date as [Last Paid Date] from member m LEFT JOIN subscription_plan sp ON m.mem_plan=sp.id where m.status='A' and due_date  < = GETDATE()", con);
            DataTable dt = new DataTable();
            // con.Open();D:\Visual studio\project\GYM\Fitness_club\addinstructor.cs
            adapter.Fill(dt);
            dataGridView1.DataSource = dt;
            if (dt.Rows.Count == 0)
            {
                buttonEdit.Enabled = false;
                buttonDelete.Enabled = false;
                buttonPay.Enabled = false;
                DialogResult result = MessageBox.Show("No Details found", "Transactions", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (result == DialogResult.OK)
                {
                    this.BeginInvoke(new MethodInvoker(Close));
                }

            }
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            this.Hide();
            addpladfn edit = new addpladfn(getGridSelectedMemberId());
            // edit.setEditMemberId(getGridSelectedMemberId());
            edit.MdiParent = this.ParentForm;
            edit.Show();
        }

        private int getGridSelectedMemberId()
        {
            int memberId = -1;
            int index = dataGridView1.CurrentCell.RowIndex;
            memberId = int.Parse(dataGridView1.Rows[index].Cells[0].Value.ToString());
            return memberId;
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            
            try
            {
                // objCon.Open();
                con.Open();

                SqlCommand objCmd = new SqlCommand();
                objCmd.Connection = con;

                SqlParameter param = new SqlParameter();

                objCmd.CommandType = CommandType.StoredProcedure;
                objCmd.CommandText = "pr_delete_member";

                param = new SqlParameter();
                param.ParameterName = "@id";
                param.SqlDbType = SqlDbType.Int;
                param.Value = getGridSelectedMemberId();
                objCmd.Parameters.Add(param);
                objCmd.ExecuteNonQuery();
                MessageBox.Show("Member deleted succesfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                if (isActiveMem)
                {
                    loadGrid(true);
                }
                if (isDeactiveMem)
                {
                    loadGrid(false);
                }
                
            }
            catch
            {
                MessageBox.Show("Member delete failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                con.Close();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            loadGridbyKeyword(textBox1.Text.ToString(),isActiveMem);
        }

        void loadGridbyKeyword(String searchKey,Boolean isActiveMem)
        {
            String status = "A";
            if (!isActiveMem)
            {
                status = "I";
            }
            SqlDataAdapter adapter = new SqlDataAdapter("select * from member where (firstname like '%" + searchKey + "%' or lastname like '%" + searchKey + "%' or contactno like '%" + searchKey + "%') AND status='" + status+"'", con);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            dataGridView1.DataSource = dt;
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            this.Hide();
            fees feesFrm = new fees(getGridSelectedMemberId());
            // edit.setEditMemberId(getGridSelectedMemberId());
            feesFrm.MdiParent = this.ParentForm;
            feesFrm.Show();
        }

        private void buttonDeactivate_Click(object sender, EventArgs e)
        {
            if (buttonDeactivate.Text == "Activate")
            {
                try
                {
                    // objCon.Open();
                    con.Open();

                    SqlCommand objCmd = new SqlCommand();
                    objCmd.Connection = con;

                    SqlParameter param = new SqlParameter();

                    objCmd.CommandType = CommandType.StoredProcedure;
                    objCmd.CommandText = "pr_activate_member";

                    param = new SqlParameter();
                    param.ParameterName = "@id";
                    param.SqlDbType = SqlDbType.Int;
                    param.Value = getGridSelectedMemberId();
                    objCmd.Parameters.Add(param);
                    objCmd.ExecuteNonQuery();
                    MessageBox.Show("Member activated succesfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadGrid(false);
                }
                catch
                {
                    MessageBox.Show("Member activate failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    con.Close();
                }

            }else if(buttonDeactivate.Text=="Deactivate"){
                try
                {
                    // objCon.Open();
                    con.Open();

                    SqlCommand objCmd = new SqlCommand();
                    objCmd.Connection = con;

                    SqlParameter param = new SqlParameter();

                    objCmd.CommandType = CommandType.StoredProcedure;
                    objCmd.CommandText = "pr_deactivate_member";

                    param = new SqlParameter();
                    param.ParameterName = "@id";
                    param.SqlDbType = SqlDbType.Int;
                    param.Value = getGridSelectedMemberId();
                    objCmd.Parameters.Add(param);
                    objCmd.ExecuteNonQuery();
                    MessageBox.Show("Member deactivated succesfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    loadGrid(true);
                }
                catch
                {
                    MessageBox.Show("Member deactivate failed, Try Again", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    con.Close();
                }
            }
            
            
        }

        private void checkBoxActive_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxActive.Checked)
            {
                checkBoxDeactive.Checked = false;
                isActiveMem = true;
                buttonDeactivate.Text = "Deactivate";
                loadGrid(true);
            }
            else
            {
                isActiveMem = false;
            }
           
        }

        private void checkBoxDeactive_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxDeactive.Checked)
            {
                checkBoxActive.Checked = false;
                isDeactiveMem = true;
                buttonDeactivate.Text = "Activate";
                loadGrid(false);
            }
            else
            {
                isDeactiveMem = false;
            }
            
        }
    }
}
